package com.ruoyi.web.sso.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.web.sso.common.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.sso.mapper.CardMapper;
import com.ruoyi.web.sso.domain.Card;
import com.ruoyi.web.sso.service.ICardService;
import com.ruoyi.common.core.text.Convert;

/**
 * 系统卡Service业务层处理
 *
 * @author qiufeng
 * @date 2022-10-27
 */
@Service
public class CardServiceImpl implements ICardService {
    @Autowired
    private CardMapper cardMapper;

    /**
     * 查询系统卡
     *
     * @param id 系统卡主键
     * @return 系统卡
     */
    @Override
    public Card selectCardById(String id) {
        return cardMapper.selectCardById(id);
    }

    /**
     * 查询系统卡列表
     *
     * @param card 系统卡
     * @return 系统卡
     */
    @Override
    public List<Card> selectCardList(Card card) {
        return cardMapper.selectCardList(card);
    }

    /**
     * 新增系统卡
     *
     * @param card 系统卡
     * @return 结果
     */
    @Override
    public int insertCard(Card card) {
        card.setId(UuidUtil.getUuid());
        card.setDeleted(0);
        card.setCreated(new Date());
        card.setCreatedBy(ShiroUtils.getLoginName());
        return cardMapper.insertCard(card);
    }

    /**
     * 修改系统卡
     *
     * @param card 系统卡
     * @return 结果
     */
    @Override
    public int updateCard(Card card) {
        return cardMapper.updateCard(card);
    }

    /**
     * 批量删除系统卡
     *
     * @param ids 需要删除的系统卡主键
     * @return 结果
     */
    @Override
    public int deleteCardByIds(String ids) {
        return cardMapper.deleteCardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除系统卡信息
     *
     * @param id 系统卡主键
     * @return 结果
     */
    @Override
    public int deleteCardById(String id) {
        return cardMapper.deleteCardById(id);
    }

    @Override
    public String importCard(List<Card> cardList, String logName) {
        if (StringUtils.isNull(cardList) || cardList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Card card : cardList) {

            // 验证是否存在这个用户
            Card c = cardMapper.getCardNo(card.getCardNo());
            if (StringUtils.isNull(c)) {
                this.insertCard(card);
                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + card.getCardNo() + " 导入成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、账号 " + card.getCardNo() + " 已存在");
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
